const router = require("express").Router();
const xlsx = require("node-xlsx");
// const nodeExcel = require('excel-export')
const urlencode = require("urlencode");
const sql = require("./../mysql");
const Product = require("./../mysql/collection/HomeWork");
// const utils = require('./../utils')
const uuid = require("uuid");
const HomeWork = require("./../mysql/collection/HomeWork");

/**
 * @api {get} /api/homework/list 获取商品的列表或者搜索商品
 * @apiName GeHomeWorkList
 * @apiGroup HomeWork
 * @apiParam { Number } count 页码，默认值为1
 * @apiParam {Number } limitNum 每页显示个数，默认值为10
 * @apiParam {String } search 搜索title的名字
 * @apiParam {String } time 时间
 *
 * @apiHeader {String} token token
 *
 * @apiSuccess {String} code 状态码.
 * @apiSuccess {String} message  描述
 * @apiSuccess {Number} total  总数量
 * @apiSuccess {String} data  数据
 *
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "code": "200",
 *       "message": "获取商品的列表",
 *       "total": len,
 *       "data": []
 *     }
 * @apiSampleRequest /admin/pro/list
 */
router.get("/list", (req, res, next) => {
  let { count=1, limitNum=10, search, time } = req.query;
  count *= 1;
  limitNum *= 1;
  let whereData = {};
  if (search) {
    whereData = { ...whereData, workTitle: new RegExp(search) };
  }
  if (time) {
    // whereData = { ...whereData, time };
  }
  sql
    .paging(Product, whereData, { _id: 0, __v: 0 }, count, limitNum, {
      _id: -1,
    })
    .then((data) => {
      sql.count(HomeWork, whereData).then((len) => {
        console.log(len);
        console.log(whereData);
        res.status(200).send({
          code: "200",
          message: "获取列表",
          total: len,
          data,
        });
      });
    });
});

/**
 * @api {patch} /api/homeword/:workId 修改作业信息
 * @apiName patchHomeWork
 * @apiGroup HomeWork
 *
 * @apiParam { string } workId 作业的id
 * @apiParam {Object } body 修改的数据 {workTitle,workCover,workImgs,workDesc,workScore}
 *
 * @apiSuccess {String} code 状态码.
 * @apiSuccess {String} message  描述
 * @apiHeader {String} token token
 *
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "code": "200",
 *       "message": "修改商品是否推荐或者秒杀",
 *     }
 * @apiSampleRequest /admin/pro/updateFlag
 */
router.patch("/update/:workId", (req, res, next) => {
  sql
    .update(Product, { workId: req.params.workId }, { $set: req.body })
    .then(() => {
      res.status(200).json({
        code: "200",
        message: "修改商品成功",
      });
    });
});

/**
 * @api {post} /api/homework/pub 发布作业
 * @apiName PostHomeWork
 * @apiGroup HomeWork
 *
 * @apiParam { string } workTitle 标题
 * @apiParam {string } userid 用户id
 * @apiParam {string } workCover 封面图片地址
 * @apiParam {string } workImgs 组图地址
 * @apiParam {string } workDesc 作业详细信息
 * @apiParam { string } workScore 作业分数
 *
 * @apiSuccess {String} code 状态码.
 * @apiSuccess {String} message  描述
 * @apiHeader {String} token token
 *
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "code": "200",
 *       "message": "发成功",
 *     }
 * @apiSampleRequest /api/homework/pub
 */
router.post("/pub", (req, res, next) => {
  let {
    workTitle = "",
    userid,
    workCover = "",
    workImgs = [],
    workDesc = "",
    workScore = 0,
    username=''
  } = req.body;
  let insertData = {
    workId: "homework_" + uuid.v4(),
    workTitle,
    userid,
    workCover,
    workImgs,
    workDesc,
    workScore,
    username
  };
  sql.insert(HomeWork, insertData).then(() => {
    res.status(200).send({
      code: 200,
      message: "上传成功",
    });
  });
});

/**
 * @api {delete} /api/homework/:proid 根据workid删除上传商品
 * @apiName deleteHomeWork
 * @apiGroup HomeWork
 * @apiParam { string } workId 产品的id
 * @apiSuccess {String} code 状态码.
 * @apiSuccess {String} message  描述
 * @apiHeader {String} token token
 *
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "code": "200",
 *       "message": "删除成功",
 *     }
 */
router.delete(".del/:workId", (req, res) => {
  sql.delete(Product, { workId: req.params.workId }).then(() => {
    res.status(200).send({
      code: 200,
      message: "删除成功",
    });
  });
});

module.exports = router;
